package com.catering.controller;

import com.catering.service.AIService;
import com.catering.pojo.ChatRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/ai")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class AIController {

    private static final Logger logger = LoggerFactory.getLogger(AIController.class);

    @Autowired
    private AIService aiService;

    @PostMapping("/chat")
    public ResponseEntity<String> chat(@RequestBody ChatRequest request) {
        try {
            if (request.getMessages() == null || request.getMessages().isEmpty()) {
                return ResponseEntity.badRequest().body("消息不能为空");
            }
            
            String response = aiService.chatWithAI(request.getMessages().get(0).getContent());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("处理AI对话请求时发生错误: {}", e.getMessage(), e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body("AI服务暂时不可用，请稍后再试");
        }
    }
} 